import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [

  {
    name: 'notFound',
    path: '/:path(.*)+',
    redirect: {
      name: 'goods',
    },
  },
  {
    name: 'search',
    path: '/search',
    component: () => { return import('@/view/search') },
    meta: {
      title: '搜索',
    },
  },
  {
    name: 'login',
    path: '/login',
    component: () => { return import('@/view/login') },
    meta: {
      title: '登录',
    },
  },
  {
    name: 'user',
    path: '/user',
    component: () => { return import('@/view/user') },
    meta: {
      title: '会员中心',
    },
  },
  {
    name: 'cart',
    path: '/cart',
    component: () => { return import('@/view/cart') },
    meta: {
      title: '购物车',
    },
  },
  {
    name: 'goods',
    path: '/goods',
    component: () => { return import('@/view/base') },
    meta: {
      title: '商品详情',
    },
  },
]

const router = createRouter({
  routes,
  history: createWebHashHistory(),
})

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
    document.title = title
  }
  next()
})

export { router }
